package com.hr.reactive.flux;

import com.hr.reactive.common.Utils;
import reactor.core.publisher.Flux;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class Lec01FluxJust {
    public static void main(String[] args) {
        List.of(1, 2, 3, 4, 5, 6);
        var flux = Flux.just(1, 2, 3, 4, 5);
        flux.subscribe(Utils.subscriber("01"));
        flux.
                filter(i -> i % 2 == 0).
                map(i -> i + "a").
                subscribe(Utils.subscriber("02"));

        flux.filter(i -> i > 7).
                map(i -> i + "a").subscribe(Utils.subscriber("03"));

        var list = List.of("a", "b", "c");
        Flux.fromIterable(list)
                .subscribe(Utils.subscriber("iterable"));

        //stream
        var list1 = List.of(1, 2, 3, 4);
        Stream<Integer> stream = list1.stream();
        Flux<Integer> integerFlux = Flux.fromStream(stream);
        integerFlux.subscribe(Utils.subscriber("stream"));
        //stream has already been operated upon or closed   java stream基本是一次使用，使用后不能再次用
        integerFlux.subscribe(Utils.subscriber("stream2"));
        integerFlux.subscribe(Utils.subscriber("stream3"));
        //如果用Supplier的方式就不会有这个问题 他会每个订阅者去提供一个新的生产者 供应商
        Flux<Integer> streamSupplier = Flux.fromStream(() -> list1.stream());
        streamSupplier.subscribe(Utils.subscriber("stream"));
        streamSupplier.subscribe(Utils.subscriber("stream2"));
        streamSupplier.subscribe(Utils.subscriber("stream3"));

        //range
        Flux.range(1,10).subscribe(Utils.subscriber("range"));

        Flux.range(1,10).map(i->Utils.faker().name().firstName()).subscribe(Utils.subscriber("range02"));

    }
}
